

from pages.base_page import BasePage
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

class BusinessDetailsPage(BasePage):
    url = "/crm/index.php?m=business"
    result_locator = (By.XPATH, '/html/body/div[5]/div[2]')
    error_locator =(By.XPATH,"/html/body/div/form/fieldset/div[1]")
    # 查看
    check_locator = (By.CSS_SELECTOR, '#form1 > table > tbody > tr:nth-child(1) > td:nth-child(12) > a:nth-child(1)')
    add_product_locator = (By.CSS_SELECTOR, '#right_list > ul > li:nth-child(3) > a')  # 添加产品
    product_name_locator = (By.ID, 'product_name')  # 产品
    search_locator = (By.ID, 'search')  # 产品搜索关键词
    search_button_locator = (By.CSS_SELECTOR, '#dialog-select > div > ul > li:nth-child(4) > button')  # 搜索按钮
    choose_locator = (By.CSS_SELECTOR, '#data > tr > td:nth-child(1) > input[type=radio]')  # 选择产品
    ok_locator = (By.XPATH, '/html/body/div[18]/div[3]/div/button[1]')  # ok按钮
    estimate_price_locator = (By.ID, 'estimate_price')  # 报价
    sales_price_locator = (By.ID, 'sales_price')  # 售价
    amount_locator = (By.ID, 'amount')  # 数量
    description_locator = (
    By.CSS_SELECTOR, '#dialog-product > form > table > tbody > tr:nth-child(4) > td:nth-child(2) > '
                     'textarea')  # 备注
    save_locator = (By.XPATH, '//*[@id="dialog-product"]/form/table/tfoot/tr/th[2]/input[1]')  # 保存
    # 推进
    push_locator = (By.CSS_SELECTOR,'#right_list > ul > li:nth-child(2) > a')
    stage_locator = (By.XPATH, '//*[@id="dialog-advance"]/form/table/tbody/tr[2]/td[2]/select')
    nextstep_time_locator = (By.ID, 'nextstep_time')
    nextstep_locator = (By.ID, 'nextstep')
    push_description_locator = (By.XPATH, '//*[@id="dialog-advance"]/form/table/tbody/tr[4]/td[2]/textarea')
    push_button_locator = (By.XPATH, '//*[@id="dialog-advance"]/form/table/tbody/tr[5]/td[2]/input[1]')
    tips_locator = (By.CSS_SELECTOR,'#tab1 > div.back_box.container3.mar-top > div.alert.alert-success')

    def __init__(self, driver):
        """
        重写父类的构造方法，获取到点击【查看】后进入的线索详情的资源路径，重新赋值给url
        :param driver:
        """
        super().__init__(driver)
        super().open()
        check_element = self.find_element(self.check_locator)
        self.url = check_element.get_attribute("href")
    def click_add_product(self):
        """
        点击添加产品
        :return: None
        """
        self.find_element(self.add_product_locator).click()

    def click_product_name(self):
        """
        点击产品
        :return: None
        """
        self.find_element(self.product_name_locator).click()

    def input_search(self,product_key):
        """
        输入产品搜索关键词
        :param product_key:
        :return: None
        """
        self.find_element(self.search_locator).send_keys(product_key)

    def click_search_btn(self):
        """
        点击搜索按钮
        :return: None
        """
        self.find_element(self.search_button_locator).click()

    def click_choose(self):
        """
        点击第一个搜索结果
        :return: None
        """
        self.find_element(self.choose_locator).click()

    def click_ok(self):
        """
        点击ok
        :return: None
        """
        self.find_element(self.ok_locator).click()
    def input_estimate_price(self,estimate_price):
        """
        输入报价
        :param estimate_price:
        :return: None
        """
        self.find_element(self.estimate_price_locator).send_keys(estimate_price)

    def input_sales_price(self,sale_price):
        """
        输入售价
        :param sale_price:
        :return: None
        """
        self.find_element(self.sales_price_locator).send_keys(sale_price)

    def input_amount(self,amount):
        """
        输入数量
        :param amount:
        :return: None
        """
        amount_element = self.find_element(self.amount_locator)
        amount_element.clear()
        amount_element.send_keys(amount)

    def input_description(self,description):
        """
        输入备注
        :param description:
        :return: None
        """
        self.find_element(self.description_locator).send_keys(description)

    def click_save(self):
        """
        点击保存
        :return: None
        """
        self.find_element(self.save_locator).click()

    def click_push(self):
        """
        点击推进
        :return: None
        """
        self.find_element(self.push_locator).click()

    def input_stage(self,stage):
        """
        输入前进到阶段
        :param stage:
        :return: None
        """
        self.find_element(self.stage_locator).send_keys(stage)

    def input_nextstep_time(self,nextstep_time):
        """
        输入下次联系时间
        :param nextstep_time:
        :return: None
        """
        self.find_element(self.nextstep_time_locator).send_keys(nextstep_time)

    def input_nextstep(self,nextstep):
        """
        输入下次联系内容
        :param nextstep:
        :return: None
        """
        self.find_element(self.nextstep_locator).send_keys(nextstep)

    def input_push_description(self,push_description):
        """
        输入阶段描述
        :param push_description:
        :return: None
        """
        self.find_element(self.push_description_locator).send_keys(push_description)

    def click_push_btn(self):
        """
        点击推进按钮
        :return: None
        """
        self.find_element(self.push_button_locator).click()

    def get_tips(self):
        """
        获取成功或失败提示信息
        :return: 成功或失败提示信息
        """
        return self.find_element(self.tips_locator).text

#返回增删改查后的提示信息
    def get_result_tips(self):
        return self.find_element(self.result_locator).text

#返回弹窗文本信息
    def get_alert_text(self):
        return self.driver.switch_to.alert.text

#返回报错弹窗文本信息
    def get_error_text(self):
        return self.find_element(self.error_locator).text

